package com.danskin.wechat.web;

import java.io.IOException;
import java.io.OutputStream;
import java.io.UnsupportedEncodingException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang3.StringUtils;
import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFCellStyle;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.danskin.wechat.entity.CustGiftRecord;
import com.danskin.wechat.entity.CustInfo;
import com.danskin.wechat.model.LoginUser;
import com.danskin.wechat.service.CustGiftRecordService;
import com.danskin.wechat.util.LogUtil;
import com.danskin.wechat.util.Page;
import com.danskin.wechat.vo.CustGiftRecordVo;

@Controller
@RequestMapping("custGiftRecord")
public class CustGiftRecordController extends BaseController{
	
	@Autowired
	private CustGiftRecordService custGiftRecordService;
	
	/**
	 * 跳转到奖品领取记录页面
	 * @return
	 * @author makunsmac
	 */
	@RequestMapping(value = "jumpCustGiftRecordList", method = RequestMethod.GET)
	public String jumpCustGiftRecordList() {
		return "coupon/cust_gift_record_list";
	}
	
	/**
	 * 分页获取奖品领取记录
	 * @param page
	 * @return
	 * @author makunsmac
	 */
	@ResponseBody
	@RequestMapping(value = "listPageCustGiftRecord", method = RequestMethod.GET)
	public Object listPageCustGiftRecord(Page page) {
		List<CustGiftRecordVo> scanList = new ArrayList<CustGiftRecordVo>();
		try {
			Map map = getRequestParameter(getRequest());
			LoginUser loginUser = getLoginUser();
			map.put("mpId", loginUser.getMpId());
			page.setMap(map);
			scanList = custGiftRecordService.listPageCustGiftRecord(page);

		} catch (Exception e) {
			LogUtil.error("=======分页查询电频二维码扫描记录异常", e);
		}
		return pagelistToJSONForBootStrapTable(scanList, page);
	}
	/***
	 * 礼券领取导出
	 * @param request
	 * @param response
	 * @throws UnsupportedEncodingException
	 */
	@RequestMapping(value = "exportCustGiftRecord", method = RequestMethod.GET)
	public void exportCustGiftRecord(HttpServletRequest request, HttpServletResponse response) throws UnsupportedEncodingException{
		SimpleDateFormat dateFormater = new SimpleDateFormat("yyyy-MM-dd");
		String mobile = request.getParameter("mobile");
		String startTime = request.getParameter("startTime");
		String endTime = request.getParameter("endTime");
		String mpId = String.valueOf(this.getMpId());
		List<CustGiftRecordVo> custGiftRecord = custGiftRecordService.getCustGiftRecordExport(mobile,startTime,endTime,mpId);
		HSSFWorkbook wb = new HSSFWorkbook();  
		HSSFSheet sheet = wb.createSheet("礼券领取记录统计");
		HSSFRow row = sheet.createRow((int) 0);    
		HSSFCellStyle style = wb.createCellStyle();    
        style.setAlignment(HSSFCellStyle.ALIGN_CENTER); // 创建一个居中格式 
        HSSFCell cell = row.createCell((short) 0);    
        cell.setCellValue("姓名");    
        cell.setCellStyle(style);    
        cell = row.createCell((short) 1);    
        cell.setCellValue("手机号码");    
        cell.setCellStyle(style);    
        cell = row.createCell((short) 2);    
        cell.setCellValue("奖品类型");    
        cell.setCellStyle(style);    
        cell = row.createCell((short) 3);    
        cell.setCellValue("奖品名称");    
        cell.setCellStyle(style);       
        cell = row.createCell((short) 4);    
        cell.setCellValue("领取时间");    
        cell.setCellStyle(style);
        cell = row.createCell((short) 5);    
        cell.setCellValue("活动类型");    
        cell.setCellStyle(style);
        cell = row.createCell((short) 6);    
        cell.setCellValue("详细地址");    
        cell.setCellStyle(style);
        for (int i = 0; i < custGiftRecord.size(); i++)  {
        	row = sheet.createRow((int) i + 1);  
        	CustGiftRecordVo am = custGiftRecord.get(i);
        	row.createCell((short) 0).setCellValue(am.getName());  
            row.createCell((short) 1).setCellValue(am.getMobile()); 
            if(StringUtils.isNotEmpty(am.getPrizeType())){
            	if("coupon".equals(am.getPrizeType())){
            		row.createCell((short) 2).setCellValue("门店券"); 
            	}else if("point".equals(am.getPrizeType())){
            		row.createCell((short) 2).setCellValue("积分"); 
            	}else if("product".equals(am.getPrizeType())){
            		row.createCell((short) 2).setCellValue("实物"); 
            	}
            }
            row.createCell((short) 3).setCellValue(am.getGoodsName()); 
            if(am.getGetDate()!=null){
            	 cell = row.createCell((short) 4);  
            	 cell.setCellValue(am.getGetDate());  
            } 
            row.createCell((short) 5).setCellValue(am.getActivityName());  
            row.createCell((short) 6).setCellValue("-");  
        }
        OutputStream out = null;   
        try {        
            out = response.getOutputStream();    
            String fileName = "礼券领取记录统计.xls";// 文件名        
            response.setContentType("application/x-download");
            response.setCharacterEncoding("utf-8"); 
            response.setHeader("Content-Disposition", "attachment;filename="+new String(fileName.getBytes("gbk"), "iso8859-1"));
            wb.write(out);   
        } catch (Exception e) {    
            e.printStackTrace();    
        } finally {      
            try {       
                out.close();      
            } catch (IOException e) {      
                e.printStackTrace();    
            }      
        }   
	}
}
